#!/bin/bash
# author    tyb

# 函数
function isgreen() {
    if [ $? -eq 0 ]; then
        echo -e "\e[32m$1==========>succeed\e[0m"
    else
        echo -e "\e[31m$1==========>failed\e[0m"
        exit 1
    fi
}

function print_help() {
  echo "Usage: $0 [OPTIONS]"
  echo "Options:"
  echo "  --help, -h                Display this help message."
  echo "  --name <name>             Specify the vm name. default: test"
  echo "  --image <image>           Specify the create vm images path. Required"
  echo "  --memory| -m <int: mb>    Specify the vm memory. default: 1024"
  echo "  --vcpu| -c <int>          Specify the vm vcpu. default: 1 c"
  echo "  --disk <int: gb>          Specify the vm disk. default: 10, Disk default storage location:/opt/vm/"
  echo "  --type <type>             Specify the vm type. default: centos7.0"
  echo "  Available types: Run the 'osinfo-query os' command to view the information"
  echo ""
  echo "example:"
  echo "    createvm.sh --name test --image /opt/images/ubuntu-22.04.3-live-server-amd64.iso --type ubuntu20.04"
  echo "    createvm.sh --name test --image /opt/images/CentOS-7-x86_64-DVD-2009.iso --type centos7.0"
  
}

# 获取创建系统所需的参数
while [[ $# -gt 0 ]]; do
  case "$1" in
    --help| -h)
        print_help
        exit 0
        ;;
    --name)
      if [[ -n "$2" ]]; then
        name="$2"
        shift 2
      else
        echo "Missing argument for -h option."
        exit 1
      fi
      ;;
    --type)
      if [[ -n "$2" ]]; then
        type="$2"
        shift 2
      else
        echo "Missing argument for -h option."
        exit 1
      fi
      ;;
    --image)
      if [[ -n "$2" ]]; then
        image="$2"
        shift 2
      else
        echo "Missing argument for -h option."
        exit 1
      fi
      ;;
    --memory| -m)
      if [[ -n "$2" ]]; then
        memory="$2"
        shift 2
      else
        echo "Missing argument for -h option."
        exit 1
      fi
      ;;
    --vcpu| -c)
      if [[ -n "$2" ]]; then
        vcpu="$2"
        shift 2
      else
        echo "Missing argument for -h option."
        exit 1
      fi
      ;;
    --disk)
      if [[ -n "$2" ]]; then
        disk="$2"
        shift 2
      else
        echo "Missing argument for -h option."
        exit 1
      fi
      ;;
  esac
done

#虚拟机名称，默认为test
vm_name=$name

#虚拟机类型，默认使用centos7.0类型
vm_type=$type

#使用的镜像，默认镜像使用centos7.9的镜像，必须指定的参数
vm_image=$image
#判断变量是否为空
if [ -z "$vm_image" ]; then
    echo -e "\e[31mThe image parameter is required\e[0m"
    exit 2
fi
# 判断指定的镜像文件是否存在，限制指定的镜像为有效镜像
if [ ! -f "$vm_image" ]; then
    echo -e "\e[31mThe specified image file does not exist\e[0m"
    exit 3
fi

#设置内存大小，默认创建虚拟机的内存为1G
vm_memory=$memory

#设置虚拟cpu个数，默认创建虚拟机的vcpu个数为1
vm_cpu=$vcpu

#设置磁盘大小,默认大小10个G，根据使用情况占用真实的磁盘
vm_disk=$disk

# echo "vm config info"
# echo "name: $vm_name"
# echo "type: $vm_type"
# echo "image: $vm_image"
# echo "memory: $vm_memory"
# echo "vcpu: $cpu"
# echo "disk: $vm_disk"


# 创建虚拟机
virt-install --virt-type kvm --os-type=linux --os-variant "${vm_type:-"centos7.0"}" --name "${vm_name:-"test"}" --memory "${vm_memory:-1024}"  --vcpus "${vm_cpu:-1}" --disk /opt/vm/"${vm_name}".qcow2,format=qcow2,bus=virtio,size="${vm_disk:-10}" --cdrom "${vm_image}"  --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
